package harima.collision.bodies;

public class GravitiUpdater extends BodyUpdater{

	private float gravity;
	protected float fallSpeed;
	private float maxFallSpeed;
	
	public GravitiUpdater(Body _updatedBody) {
		super(_updatedBody);
		fallSpeed = 0;
		gravity = 0;
		maxFallSpeed = updatedBody.height;
	}
	
	public GravitiUpdater(Body _updatedBody, float _gravity)
	{
		super(_updatedBody);
		fallSpeed = 0f;
		gravity = _gravity;
	}
	
	public void setGravity(float _gravity)
	{
		gravity = _gravity;
	}


	public void updateBody(long time) {
		// TODO Auto-generated method stub
		if(isFalling())
		{
			updatedBody.move(determineDirection(), fallSpeed);
			incrementFallSpeed();
		}
		else
		{
			fallSpeed = 0;
		}
	}
	
	protected boolean isFalling()
	{
		return updatedBody.canMove(determineDirection(), fallSpeed);
	}

	private void incrementFallSpeed() {
		fallSpeed += gravity;
		if(fallSpeed > maxFallSpeed)
		{
			fallSpeed = maxFallSpeed;
		}
	}
		
	private Direction determineDirection()
	{
		if(fallSpeed>0)
		{
			return Direction.Down;
		}
		else
		{
			return Direction.Up;
		}
	}

}
